import axios from "axios";

// 登录请求方法
const loginreq = (method, url, params) => {

    return axios({
        method: method,
        url: url,
        headers: {
            'Content-Type': 'application/json',
        },
        data: params,
        traditional: true,
        // transformRequest: [
        //     function(data) {
        //         let ret = ''
        //         for (let it in data) {
        //             ret +=
        //                 encodeURIComponent(it) +
        //                 '=' +
        //                 encodeURIComponent(data[it]) +
        //                 '&'
        //         }
        //      ret=ret.substring(0,ret.length-1)
        //         return ret
        //     }
        // ]
    }).then(res => res.data);
};
// 通用公用方法
const req = (method, url, params) => {
  return axios({
    method: method,
    url: url,
    headers: {
      "Content-Type": "application/x-www-form-urlencoded",
      token: localStorage.getItem("logintoken"),
    },
    data: params,
    traditional: false,
    transformRequest: [
      function (data) {
        let ret = "";
        for (let it in data) {
          ret +=
            encodeURIComponent(it) + "=" + encodeURIComponent(data[it]) + "&";
        }
        return ret;
      },
    ],
  }).then((res) => res.data);
};
//通讯接口的新增
const reqAdd = (method, url, params) => {
    return axios({
      method: method,
      url: url,
      headers: {
        "Content-Type": "application/x-www-form-urlencoded",
        token: localStorage.getItem("logintoken"),
      },
      data: params,
      // traditional: false,
      // transformRequest: [
      //   function (data) {
      //     let ret = "";
      //     for (let it in data) {
      //       ret +=
      //         encodeURIComponent(it) + "=" + encodeURIComponent(data[it]) + "&";
      //     }
      //     return ret;
      //   },
      // ],
    }).then((res) => res.data);
  };
// 点表---渲染数据
const reqDian = (method, url, params) => {
  return axios({
    method: method,
    url: url,
    headers: {
      "Content-Type": "application/x-www-form-urlencoded",
      token: localStorage.getItem("logintoken"),
    },
    params: params,
    traditional: false,
    transformRequest: [
      function (data) {
        let ret = "";
        for (let it in data) {
          ret +=
            encodeURIComponent(it) + "=" + encodeURIComponent(data[it]) + "&";
        }
        return ret;
      },
    ],
  }).then((res) => res.data);
};
export { loginreq, req, reqAdd, reqDian};
